﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Web;
using System.IO;

namespace OgilvyOne.BLL
{
    public class MailService
    {
        //sendMail(收件人邮箱 邮件内容 邮件主题)
        public static bool sendMail(string toMail, string emailBody, string subject)
        {
            StreamReader sr = null;
            try
            {
                MailMessage dim = new MailMessage();
                //new MailAddress("发件人邮箱", "发件人名称");
                dim.From = new MailAddress("importgolf@vw.com.cn", "大众进口汽车高尔夫");
                dim.To.Add(toMail);
                dim.Subject = subject;
                dim.Body = emailBody;
                dim.IsBodyHtml = true;
                dim.BodyEncoding = System.Text.Encoding.UTF8;
                dim.Priority = System.Net.Mail.MailPriority.Normal;
                SmtpClient smtpClient = new SmtpClient();
                //发件人密码
                smtpClient.Credentials = new System.Net.NetworkCredential(dim.From.Address, "importgolfmail");
                smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                //邮件发送服务器
                smtpClient.Host = "mail.vw.com.cn";
                smtpClient.Send(dim);
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                    sr.Dispose();
                }
            }
            return true;
        }
        public static string generateEmail(string userName, string code,string param1,string param2,string templateName)
        {
            StreamReader sr = new StreamReader(getPath(string.Format("{0}.html", templateName)));
            string str = null;
            try
            {
                str = sr.ReadToEnd();
                str = str.Replace("$userName$", userName);
                str = str.Replace("$code$", code);
                str = str.Replace("$param1$", param1);
                str = str.Replace("$param2$", param2);
            }
            catch (Exception e)
            {
                e.ToString();
            }
            finally
            {
                sr.Close();
            }
            return str;
        }
        private static string getPath(string path)
        {
            return HttpContext.Current.Server.MapPath(path);
        }
    }

}
